#pragma once
#ifndef xDxApp_h__
#define xDxApp_h__

#endif // xDxApp_h__
#include "../../../dxhead/dxegheader.h"
#include "../../../mylib.h"
#include "xState.h"
#include "../../tools/inc/xWindow.h"
#include "../../tools/inc/xD3DEnv.h"
#include "../../tools/inc/xDevice.h"
namespace Sky
{
	class xDxApp : public Object
	{
		DECL_RTTI1(xDxApp,Object);
	protected:
		HINSTANCE m_hInstance;
		StringPtr	m_AppName;
		//------------------------------------------------------------------------------------
		//object handle the state of the app
		//------------------------------------------------------------------------------------
		xState m_state;
		//------------------------------------------------------------------------------------
		//window
		//------------------------------------------------------------------------------------
		xWindow m_window;
		//------------------------------------------------------------------------------------
		//3denv
		//------------------------------------------------------------------------------------
		xD3DEnv m_d3denv;
		//------------------------------------------------------------------------------------
		//device
		//------------------------------------------------------------------------------------
		xDevice m_device;
	public:
		DXAPI xDxApp(TVoid);
		DXAPI virtual ~xDxApp(TVoid);

	public:
		DXAPI StringPtr&	getAppName(){return m_AppName;}
		DXAPI xD3DEnv*	getD3DEnv();
		DXAPI xWindow*	getWindow();
		DXAPI xDevice*	getDevice();
		DXAPI xState*   getState();;
	public:
		//------------------------------------------------------------------------------------
		//Create the 3d environment
		//------------------------------------------------------------------------------------
		DXAPI TResult Create3DEnv();
		//------------------------------------------------------------------------------------
		//Create the main window,then use Create3DDevice()
		//------------------------------------------------------------------------------------

		DXAPI TResult Create3DWindow(const TChar* strWindowTitle = (TChar*)TEXT("dxeg"),
			TUint dwWinStyle=WS_POPUP,
			TInt nDefaultX=0,TInt nDefaultY=0,
			TInt nDefaultWidth=1024,TInt nDefaultHeight=768,
			WNDPROC winProc=vNull);
		//------------------------------------------------------------------------------------
		//Set the window that will be rendered,then use Create3DDevice()
		//------------------------------------------------------------------------------------
		DXAPI TResult Set3DWindow(HWND hWnd,TBool handlemsg=vFalse);
		//------------------------------------------------------------------------------------
		//Create the device,then use Main loop()
		//------------------------------------------------------------------------------------
		DXAPI TResult Create3DDevice(UINT AdapterOrdina=D3DADAPTER_DEFAULT, TBool bWindowed=vTrue,
			TInt nSuggestedBufferWidth=1024, TInt nSuggestedBufferHeight=768);
		//------------------------------------------------------------------------------------
		//The message loop
		//------------------------------------------------------------------------------------
		DXAPI TResult MainLoop(TBool bWinProc = vFalse,TBool need3D = vFalse);
		//------------------------------------------------------------------------------------
		//Get the exit code
		//------------------------------------------------------------------------------------
		DXAPI TInt		GetExitCode(){return m_state.m_ExitCode;};
		//------------------------------------------------------------------------------------
		//Set the exit code
		//------------------------------------------------------------------------------------
		DXAPI TVoid	SetExitCode(ExitCode code){m_state.m_ExitCode=code;};
		//------------------------------------------------------------------------------------
		//Set the TResult code
		//------------------------------------------------------------------------------------
		DXAPI TVoid SetResult(TInt hr){m_state.m_hr = hr;}
		//------------------------------------------------------------------------------------
		//Set the TResult code
		//------------------------------------------------------------------------------------
		DXAPI TResult GetResult(){ return m_state.m_hr;}
		//------------------------------------------------------------------------------------
		//Set the TResult code
		//------------------------------------------------------------------------------------
		DXAPI TVoid	ShowWindow(TBool bShow = vTrue){m_window.show(bShow);}
		//
		DXAPI ExitCode Exit();
	public:
		//------------------------------------------------------------------------------------
		//Called to rendered object
		//in XApp::OnRenderSceneLoop(),Objects added called OnRenderSceneLoop() function
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult	RenderSceneLoop(){return XE_OK;};
		//------------------------------------------------------------------------------------
		//Called to handle message
		//in XApp::OnHandlerMessageLoop(),Objects added called OnHandlerMessageLoop() function
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult	HandleMessageLoop(){return XE_OK;};
		//------------------------------------------------------------------------------------
		//Called to handle message
		//in XApp::HandleWinPorc(),Objects added called HandleWinPorc() function
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult	HandleWinPorc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){return XE_OK;};
	protected:
		//------------------------------------------------------------------------------------
		//Inherit from XResourceObject
		//Called when objects created
		//in XApp::onDeviceCreate() m_ResourceMgr.OnDeviceCreate() are called
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult onDeviceCreate(xDevice* pDevice){return XE_OK;};
		//------------------------------------------------------------------------------------
		//Inherit from XResourceObject
		//Called when objects reseted
		//in XApp::onDeviceReset() m_ResourceMgr.OnDeviceReset() are called
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult	onDeviceReset(xDevice* pDevice){return XE_OK;};
		//------------------------------------------------------------------------------------
		//Inherit from XResourceObject
		//Called when objects lost
		//in XApp::onDeviceLost() m_ResourceMgr.OnDeviceLost() are called
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult	onDeviceLost(){return XE_OK;};
		//------------------------------------------------------------------------------------
		//Inherit from XResourceObject
		//Called when objects destroyed
		//in XApp::onDeviceDestroy() m_ResourceMgr.OnDeviceDestroy() are called
		//------------------------------------------------------------------------------------
		DXAPI virtual TResult	onDeviceDestroy(){return XE_OK;};
		//
		DXAPI virtual TResult   onChangeSettings(xDevice* pDevice);

	public:
		//------------------------------------------------------------------------------------
		//Called when having a  message
		//------------------------------------------------------------------------------------
		//virtual TVoid    OnMessage(xEventMessage* pMsgInfo);
		//------------------------------------------------------------------------------------
		//Force the engine to exit,and release pd3ddevice
		//------------------------------------------------------------------------------------
		DXAPI TVoid	Shutdown(ExitCode code = ExitCode_Exit);
		//------------------------------------------------------------------------------------
		//use positive number to increase the times paused,
		//and negative number to decrease the times paused
		//------------------------------------------------------------------------------------
		DXAPI TVoid	AddPauseCount(TInt PauseTimeCount=0,TInt PauseRenderCount=0);
	private:
		TVoid	InitState();
		TVoid	UninitState();

	public:
		//------------------------------------------------------------------------------------	
		//Create device ,XIObjects are created automatically
		//------------------------------------------------------------------------------------	
		DXAPI TResult	Create3DObjects(); 
		//------------------------------------------------------------------------------------
		//Reset device ,XIObjects reseted automatically
		//------------------------------------------------------------------------------------
		DXAPI TResult	Reset3DObjects();
		//------------------------------------------------------------------------------------
		//Handle Timer and Device state, XIObjects rendered automatically
		//------------------------------------------------------------------------------------
		DXAPI TVoid	Render3DObjects(); 
		//------------------------------------------------------------------------------------
		//Release Device , XIObjects lost and destroy automatically
		//------------------------------------------------------------------------------------
		DXAPI TResult	Delete3DObjects();	
	};
}
